/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.app;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import jdbcmanager.swing.*;

/**
 * 
 * @author Francisco Morero Peyrona
 */
final class ConfigTabProxy extends JPanel
{
   private HttpProxy   http;
   private SocketProxy socket;
   
   //-------------------------------------------------------------------------//
   
   ConfigTabProxy()
   {
      init();
   }
   
   void save()
   {
      this.http.save();
      this.socket.save();
   }
   
   private void init()
   {
      this.http   = new HttpProxy();
      this.socket = new SocketProxy();
      
      setLayout( new BorderLayout( 0,7 ) );
      
      add( this.http  , BorderLayout.NORTH );
      add( this.socket, BorderLayout.SOUTH );
   }
   
   //-------------------------------------------------------------------------//
   // INNER CLASS: HttpProxy
   //-------------------------------------------------------------------------//
   
   private final class HttpProxy extends JPanel
   {
      private JCheckBox  chkUseHttp;
      private JTextField txtServer;
      private JTextField txtPort;
      private JTextField txtUser;
      private JTextField txtPassword;
      private JTextField txtExclude; 
      
      private HttpProxy()
      {
         init();
      }
      
      private void checkEnabledStatus()
      {
         boolean bChecked = this.chkUseHttp.isSelected();
               
         this.txtServer.setEnabled( bChecked );
         this.txtPort.setEnabled( bChecked );
         this.txtUser.setEnabled( bChecked );
         this.txtPassword.setEnabled( bChecked );
         this.txtExclude.setEnabled( bChecked );
      }
      
      private void init()
      {
         this.chkUseHttp = new JCheckBox( "Use proxy" );
         this.chkUseHttp.setSelected( App.props().getProxyHttpActive( false ) );
         this.chkUseHttp.addActionListener( new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               checkEnabledStatus();
            }
         } );
         this.txtServer   = new JTextField( App.props().getProxyHttpServer( "" ) );
         this.txtPort     = new JTextField( App.props().getProxyHttpPort( "" ) );
         this.txtUser     = new JTextField( App.props().getProxyHttpUser( "" ) );
         this.txtPassword = new JTextField( App.props().getProxyHttpPassword( "" ) );
         this.txtExclude  = new JTextField( App.props().getProxyHttpExclude( "" ) );
         
         checkEnabledStatus();
         
         setBorder( BorderFactory.createTitledBorder( " HTTP Proxy " ) );
         setLayout( new SpringLayout() );
            
         add( this.chkUseHttp );
         add( new JLabel() );    // Trick para encuadrar
         
         add( new JLabel( "Server" ) );
         add( this.txtServer );
            
         add( new JLabel( "Port" ) );
         add( this.txtPort );
            
         add( new JLabel( "User" ) );
         add( this.txtUser ); 
            
         add( new JLabel( "Password" ) );
         add( this.txtPassword ); 
            
         add( new JLabel( "No proxy for" ) );
         add( this.txtExclude );
         
         SpringUtilities.makeCompactGrid( this, 6,2,3,8,3,8 );
      }
      
      private void save()
      {
         App.props().setProxyHttpActive(   this.chkUseHttp.isSelected()      );
         App.props().setProxyHttpServer(   this.txtServer.getText().trim()   );
         App.props().setProxyHttpPort(     this.txtPort.getText().trim()     );
         App.props().setProxyHttpUser(     this.txtUser.getText().trim()     );
         App.props().setProxyHttpPassword( this.txtPassword.getText().trim() );
         App.props().setProxyHttpExclude(  this.txtExclude.getText().trim().replace( ';' , '|' ) );
         
         App.configureProxy();
      }
   }
   
   //-------------------------------------------------------------------------//
   // INNER CLASS: SocketProxy
   //-------------------------------------------------------------------------//
   
   private final class SocketProxy extends JPanel
   {
      private JCheckBox  chkUseSocks;
      private JTextField txtServer;
      private JTextField txtPort;
     
      private SocketProxy()
      {
         init();
      }

      private void checkEnabledStatus()
      {
         boolean bChecked = this.chkUseSocks.isSelected();
         
         this.txtServer.setEnabled( bChecked );
         this.txtPort.setEnabled( bChecked );
      }
            
      private void init()
      {
         this.chkUseSocks = new JCheckBox( "Use proxy" );
         this.chkUseSocks.setSelected( App.props().getProxySocksActive( false ) );
         this.chkUseSocks.addActionListener( new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               checkEnabledStatus();
            }
         } );
         this.txtServer = new JTextField( App.props().getProxySocksServer( "" ) );
         this.txtPort   = new JTextField( App.props().getProxySocksPort( "" ) );
         
         checkEnabledStatus();
         
         setBorder( BorderFactory.createTitledBorder( " SOCKS Proxy " ) );
         setLayout( new SpringLayout() );
            
         add( this.chkUseSocks );
         add( new JLabel() );    // Trick para encuadrar
         
         add( new JLabel( "Server" ) );
         add( this.txtServer );
            
         add( new JLabel( "Port" ) );
         add( this.txtPort );
         
         SpringUtilities.makeCompactGrid( this, 3,2,3,8,3,8 );
      }

      private void save()
      {
         App.props().setProxySocksActive( this.chkUseSocks.isSelected()   );
         App.props().setProxySocksServer( this.txtServer.getText().trim() );
         App.props().setProxySocksPort(   this.txtPort.getText().trim()   );
      }
   }
}